
clear all	
cd "${data}"	
use "CHES_original.dta", clear


rename lrgen CHES_leftright
rename lrecon CHES_economicLR
rename galtan CHES_galtan 
rename electionyear CHES_electionyear
rename party_id CHES_party_id
rename year CHES_year
rename cmp_id CHES_cmp_id


gen CSES_year = .
replace CSES_year = 2008 if country==13 & CHES_year==2010 // Austria 
replace CSES_year = 2013 if country==13 & CHES_year==2014 // Austria *
replace CSES_year = 2017 if country==13 & CHES_year==2019 // Austria
replace CSES_year = 1999 if country==1 & CHES_year==1999 // Belgium *
replace CSES_year = 2003 if country==1 & CHES_year==2002 // Belgium *
replace CSES_year = 2019 if country==1 & CHES_year==2019 // Belgium *
replace CSES_year = 2002 if country==3 & CHES_year==2002 // Germany *
replace CSES_year = 1998 if country==3 & CHES_year==1999 // Germany *
replace CSES_year = 2009 if country==3 & CHES_year==2010 // Germany *
replace CSES_year = 2013 if country==3 & CHES_year==2014 // Germany *
replace CSES_year = 2017 if country==3 & CHES_year==2019 // Germany
replace CSES_year = 2007 if country==2 & CHES_year==2006 // Denmark *
replace CSES_year = 2019 if country==2 & CHES_year==2019 // Denmark *
replace CSES_year = 1996 if country==5 & CHES_year==1999 // Spain
replace CSES_year = 2004 if country==5 & CHES_year==2002 // Spain
replace CSES_year = 2008 if country==5 & CHES_year==2006 // Spain
replace CSES_year = 2007 if country==14 & CHES_year==2006 // Finland *
replace CSES_year = 2011 if country==14 & CHES_year==2010 // Finland *
replace CSES_year = 2015 if country==14 & CHES_year==2014 // Finland *
replace CSES_year = 2007 if country==6 & CHES_year==2006 // France *
replace CSES_year = 2012 if country==6 & CHES_year==2010 // France
replace CSES_year = 2017 if country==6 & CHES_year==2019 // France
replace CSES_year = 1997 if country==11 & CHES_year==1999 // UK
replace CSES_year = 2005 if country==11 & CHES_year==2006 // UK *
replace CSES_year = 2015 if country==11 & CHES_year==2014 // UK *
replace CSES_year = 2017 if country==11 & CHES_year==2019 // UK 
replace CSES_year = 2009 if country==4 & CHES_year==2010 // Greece *
replace CSES_year = 2015 if country==4 & CHES_year==2014 // Greece *
replace CSES_year = 2019 if country==4 & CHES_year==2019 // Greece *
replace CSES_year = 2002 if country==7 & CHES_year==2002 // Ireland *
replace CSES_year = 2007 if country==7 & CHES_year==2006 // Ireland *
replace CSES_year = 2011 if country==7 & CHES_year==2010 // Ireland *
replace CSES_year = 2016 if country==7 & CHES_year==2014 // Ireland
replace CSES_year = 2018 if country==8 & CHES_year==2019 // Italy *
replace CSES_year = 1998 if country==10 & CHES_year==1999 // Netherlands *
replace CSES_year = 2002 if country==10 & CHES_year==2002 // Netherlands *
replace CSES_year = 2006 if country==10 & CHES_year==2006 // Netherlands *
replace CSES_year = 2017 if country==10 & CHES_year==2014 // Netherlands 
replace CSES_year = 2021 if country==10 & CHES_year==2019 // Netherlands
replace CSES_year = 2019 if country==12 & CHES_year==2019 // Portugal  *
replace CSES_year = 1998 if country==16 & CHES_year==1999 // Sweden *
replace CSES_year = 2002 if country==16 & CHES_year==2002 // Sweden *
replace CSES_year = 2006 if country==16 & CHES_year==2006 // Sweden *
replace CSES_year = 2014 if country==16 & CHES_year==2014 // Sweden *
replace CSES_year = 2018 if country==16 & CHES_year==2019 // Sweden *



keep CHES_cmp_id CHES_electionyear CHES_leftright CHES_economicLR CHES_galtan CHES_party_id CHES_year CSES_year // year is the year of evaluation but this could be different than election year

drop if CSES_year==.

gen CHES_key = string(CHES_party_id)+"_"+string(CSES_year) 


* lrecon is economic dimension and galtan is the new politics

** ATTENTION! "party_id" is the key variable to merging with CSES_IMD

duplicates drop CHES_key, force 

save "CHES_coded.dta", replace



use "d2.dta", clear 

gen CHES_key = string(IMD5102_)+"_"+string(IMD1008_YEAR) 

merge m:1 CHES_key using "CHES_coded.dta"

* Germany 2021
replace CHES_leftright = 3.6190476 if CHES_key== "302_2021"
replace CHES_leftright = 6.4285712 if CHES_key== "303_2021"
replace CHES_leftright = 3.2380953 if CHES_key== "304_2021"
replace CHES_leftright = 1.4285715 if CHES_key== "306_2021"
replace CHES_leftright = 7.1904764 if CHES_key== "308_2021"
replace CHES_leftright = 9.2380953 if CHES_key== "310_2021"

*Spain 2000
replace CHES_leftright = 4.125 if CHES_key== "501_2000" 	// same as 1996 
replace CHES_leftright = 6.625 if CHES_key== "502_2000"
replace CHES_leftright = 2.125 if CHES_key== "504_2000"
replace CHES_leftright = 6.125 if CHES_key== "505_2000"
replace CHES_leftright = 5.75 if CHES_key== "506_2000"

*UK 2019
replace CHES_leftright = 7.1176472 if CHES_key== "1101_2019"
replace CHES_leftright = 1.9411764 if CHES_key== "1102_2019"
replace CHES_leftright = 4.2352943 if CHES_key== "1104_2019"
replace CHES_leftright = 3.5 if CHES_key== "1105_2019"
replace CHES_leftright = 3.090909 if CHES_key== "1106_2019"
replace CHES_leftright = 2 if CHES_key== "1107_2019"
replace CHES_leftright = 8.6875 if CHES_key== "1110_2019"


drop if _merge==2
drop _merge 

gen CHES_1year = 1
replace CHES_1year = 0 if election=="AUT_2008" | election=="AUT_2017" | election=="DEU_1998" | election=="DEU_2009" | election=="DEU_2017" | election=="ESP_1996" | election=="FRA_2017" | election=="GBR_1997" | election=="GBR_2005" | election=="GBR_2017" | election=="GRC_2009" | election=="IRL_2016" | election=="ITA_2018" | election=="NLD_1998" | election=="NLD_2017" | election=="SWE_1998" | election=="SWE_2018" 

save "d2.dta", replace 

